﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WikiTrailCreator.TrailImages;

namespace WikiTrailCreator.TrailImages
{
    public partial class ucPhotoGalleryGroup : UserControl
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public ucPhotoGalleryGroup()
        {
            InitializeComponent();
        }

        public void ListenToGlobalGallery()
        {
            GlobalImageGalleryContainer.Instance.GalleryChanged += new EventHandler(OnGalleryChanged);
        }

        private void buttonMoveUp_Click(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxGalleryImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                int indexOfSelected = listBoxGalleryImages.Items.IndexOf(selectedItem);

                if (indexOfSelected > 0)
                {
                    listBoxGalleryImages.Items.Remove(selectedItem);
                    listBoxGalleryImages.Items.Insert(indexOfSelected - 1, selectedItem);
                    listBoxGalleryImages.SelectedItem = selectedItem;
                }
            }
        }

        private void buttonMoveDown_Click(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxGalleryImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                int indexOfSelected = listBoxGalleryImages.Items.IndexOf(selectedItem);

                if (indexOfSelected < listBoxGalleryImages.Items.Count - 1)
                {
                    listBoxGalleryImages.Items.Remove(selectedItem);
                    listBoxGalleryImages.Items.Insert(indexOfSelected + 1, selectedItem);
                    listBoxGalleryImages.SelectedItem = selectedItem;
                }
            }
        }

        private void listBoxGalleryImages_SelectedValueChanged(object sender, EventArgs e)
        {
            
        }

        private void buttonAddImage_Click(object sender, EventArgs e)
        {
            FormImageSelector formImageSelector = new FormImageSelector();
            DialogResult dialogResult = formImageSelector.ShowDialog(this);

            if (dialogResult == DialogResult.OK)
            {
                TrailImage[] selectedItems = formImageSelector.SelectedImages;

                if (selectedItems.Length > 0)
                {
                    listBoxGalleryImages.Items.AddRange(selectedItems);
                    listBoxGalleryImages.SelectedItem = selectedItems[0];
                }
            }

            formImageSelector.Dispose();
        }

        public ImageGallery ImagesInGallery
        {
            get
            {
                return new ImageGallery(listBoxGalleryImages.Items.Cast<TrailImage>().ToArray());
            }
            set
            {
                listBoxGalleryImages.Items.Clear();

                if (value != null && value.Images.Length > 0)
                {
                    listBoxGalleryImages.Items.AddRange(value.Images);
                    listBoxGalleryImages.SelectedItem = value.Images[0];
                }
                else

                listBoxGalleryImages_SelectedIndexChanged(this, new EventArgs());
            }
        }

        private void listBoxGalleryImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxGalleryImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                buttonDeleteSelected.Enabled = true;
                labelImageBox.Image = selectedItem.Image;
            }
            else
            {
                buttonDeleteSelected.Enabled = false;
                labelImageBox.Image = null;
            }
        }

        private void buttonDeleteSelected_Click(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxGalleryImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                listBoxGalleryImages.Items.Remove(selectedItem);
            }
        }

        private void OnGalleryChanged(object sender, EventArgs e)
        {
            bool bRemovedItems = false;

            // The main gallery changed, so remove any missing entries from our
            // local gallery here...
            List<TrailImage> allImages = new List<TrailImage>(GlobalImageGalleryContainer.Instance.Images);
            TrailImage[] imagesInThisControl = listBoxGalleryImages.Items.Cast<TrailImage>().ToArray();

            foreach (TrailImage trailImage in imagesInThisControl)
            {
                if (!allImages.Contains(trailImage))
                {
                    listBoxGalleryImages.Items.Remove(trailImage);
                    bRemovedItems = true;
                }
            }

            if (bRemovedItems)
            {
                MessageBox.Show(this, "Image(s) were removed from a gallery, since they were deleted from the main image list.", "Warning", MessageBoxButtons.OK);
            }
        } 
    }
}
